package com.gm.wholesale.util;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;

public class ViewUtil {

    public static void showPrintError() {
        ToastUtil.showToast("连接打印机失败,请检查打印机是否正常");
    }

    public static void showWeighError() {
        ToastUtil.showToast("连接蓝牙秤失败，请重试或者重启蓝牙秤");
    }

    public static String getString(Context context, int id) {
        if (context == null) return "";
        return context.getResources().getString(id);
    }

    public static void initWebView(WebView mViewView) {
        WebSettings webSettings = mViewView.getSettings();
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setAllowFileAccess(true);
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
        //支持
        webSettings.setDomStorageEnabled(true);
        //禁用缓存
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        webSettings.setBlockNetworkImage(false);//解决图片不显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webSettings.setUseWideViewPort(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setLoadWithOverviewMode(true);
//其他细节操作
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

    }


    public static void startFragment(FragmentActivity activity, Fragment fragment) {
        try {
            FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.mainView, fragment);
            transaction.commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void finishFragment(FragmentActivity activity, Fragment fragment) {
        try {
            FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
            transaction.remove(fragment);
            transaction.commitAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void backFragment(FragmentActivity activity) {
        try {
            FragmentManager fragmentManager=activity.getSupportFragmentManager();
            int size=fragmentManager.getBackStackEntryCount();
           if (size>0){
               fragmentManager.popBackStack();
           }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static boolean canBack(FragmentActivity activity){
        if (getBackStackEntryCount(activity)>0){
            return true;
        }
        return false;
    }

    public static int getBackStackEntryCount(FragmentActivity activity){
        FragmentManager fragmentManager=activity.getSupportFragmentManager();
        int size=fragmentManager.getBackStackEntryCount();
        return size;
    }
}
